package cn.iocoder.yudao.module.member.service.signin;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.SignInCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.SignInExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.SignInPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.SignInUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.signin.SignInConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.SignInDO;
import cn.iocoder.yudao.module.member.dal.mysql.signin.SignInMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SIGN_IN_NOT_EXISTS;

/**
 * 用户签到 Service 实现类
 *
 * @author LinAllen
 */
@Service
@Validated
public class SignInServiceImpl implements SignInService {

    @Resource
    private SignInMapper signInMapper;

    @Override
    public Long createSignIn(SignInCreateReqVO createReqVO) {
        // 插入
        SignInDO signIn = SignInConvert.INSTANCE.convert(createReqVO);
        signInMapper.insert(signIn);
        // 返回
        return signIn.getId();
    }

    @Override
    public void updateSignIn(SignInUpdateReqVO updateReqVO) {
        // 校验存在
        validateSignInExists(updateReqVO.getId());
        // 更新
        SignInDO updateObj = SignInConvert.INSTANCE.convert(updateReqVO);
        signInMapper.updateById(updateObj);
    }

    @Override
    public void deleteSignIn(Long id) {
        // 校验存在
        validateSignInExists(id);
        // 删除
        signInMapper.deleteById(id);
    }

    private void validateSignInExists(Long id) {
        if (signInMapper.selectById(id) == null) {
            throw exception(SIGN_IN_NOT_EXISTS);
        }
    }

    @Override
    public SignInDO getSignIn(Long id) {
        return signInMapper.selectById(id);
    }

    @Override
    public List<SignInDO> getSignInList(Collection<Long> ids) {
        return signInMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SignInDO> getSignInPage(SignInPageReqVO pageReqVO) {
        return signInMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SignInDO> getSignInList(SignInExportReqVO exportReqVO) {
        return signInMapper.selectList(exportReqVO);
    }

}
